<?php

class Core_Model_DbTable_GeneralInformation extends Zend_Db_Table_Abstract
{

    protected $_name = 'general_information';

    public function get($id)
    {
        return $this->fetchRow("id = $id");
    }

    public function getFullJoin($id = -1)
    {
        if ($id == -1 || empty($id)) {
            return null;
        }

        $role = Core_Util::getRole();
        $where = " 1 = 1";
        $session = Zend_Registry::get("session");
        $user_id = isset($session->user)?$session->user->id:null;

        switch(strtolower($role)){
            case Core_Role::ROLE_PRINCIPAL:
                $where = " i.customer_id = {$user_id} ";
                break;
            case Core_Role::ROLE_INPUT:
                $where = " i.id in (select * from transactions where input_user = {$user_id}) ";
                break;
            case Core_Role::ROLE_ADMIN:
                break;
            case Core_Role::ROLE_GUEST:
            default:
                return null;
        }

        return $this->getAdapter()->fetchRow("
            select i.*, u.fullname, p.name as port_name, b.name as buoy_name
            from $this->_name i
            left join users u on u.id = i.customer_id
            left join ports p on p.id = i.port_id
            left join buoy  b on b.id = i.buoy_id
            where i.id = {$id} and {$where}
        ");
    }

    public function getMulti($ids)
    {
        if (!empty($ids)) {
            $cond = $this->getAdapter()->quoteInto("id IN (?)", $ids);
            $infos = $this->fetchAll($cond)->toArray();
            $result = array();
            foreach ($infos as $info) {
                $result[$info['id']] = $info;
            }
            return $result;
        }
        return array();
    }

}

?>
